package com.ziq.zcamp.place

import android.annotation.SuppressLint
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.viewpager2.widget.ViewPager2
import com.donkingliang.groupedadapter.holder.BaseViewHolder
import com.facebook.imagepipeline.request.ImageRequest
import com.ziq.base.extension.click
import com.ziq.zcamp.bean.place.DetailGroup
import com.ziq.zcamp.bean.place.VrInfo
import com.ziq.zcamp.bean.post.ImageInfo
import com.ziq.zcamp.databinding.ItemPlaceDetailBannerImageBinding
import com.ziq.zcamp.databinding.ItemPlaceDetailBannerVrBinding
import com.ziq.zcamp.extension.pageClick
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.mine.PicturePreviewActivity
import com.ziq.zcamp.util.ImageUtil

class PlaceDetailBannerGroup(val adapter: PlaceDetailBannerAdapter) : DetailGroup() {
    override fun getHeaderViewType(groupPosition: Int): Int {
        return 0
    }

    override fun getChildViewType(groupPosition: Int, childPosition: Int): Int {
        if(childPosition == 0 && vr!= null){
            return PlaceDetailBannerAdapter.TYPE_VR
        } else {
            return PlaceDetailBannerAdapter.TYPE_IMAGE
        }
    }

    private var imageList: MutableList<ImageInfo>? = null
    private var vr: VrInfo? = null
    var currentImagePosition = 0
    var imageSize = 0

    var onImageBannerPageSelected : ((index : Int, size : Int)->Unit)? = null

    var onNestedPreScroll : ((
        coordinatorLayout: CoordinatorLayout,
        child: View,
        target: View,
        dx: Int,
        dy: Int,
        consumed: IntArray,
        type: Int ) -> Unit )? = null

    var onStopNestedScroll : ((
        coordinatorLayout: CoordinatorLayout,
        child: View,
        target: View,
        type: Int ) -> Unit )? = null

    fun setBannerData(imageList: MutableList<ImageInfo>?, vr:VrInfo?) {
        this.imageList = imageList
        this.vr = vr
        imageSize = imageList?.size?:0
    }

    override fun getChildrenCount(): Int {
        var size = 0
        if(vr!= null){
            size++
        }
        if(imageList != null){
            size++
        }
        return size
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return false
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return false
    }

    override fun onBindHeaderViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {
    }

    override fun onBindChildViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {
        val viewType = getChildViewType(groupPosition, childPosition)
        when (viewType) {
            PlaceDetailBannerAdapter.TYPE_VR -> {
                onBindVr(holder, groupPosition, childPosition)
            }
            PlaceDetailBannerAdapter.TYPE_IMAGE -> {
                onBindImage(holder, groupPosition, childPosition)
            }
        }

    }

    private fun onBindVr(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ){
        val item = vr
        if(item != null){
            holder?.let {
                val binding = ItemPlaceDetailBannerVrBinding.bind(holder.itemView)
                holder.itemView.click {
                    adapter.onVrClickCallback?.invoke(vr!!)
                }
                ImageUtil.updateImageFromNetwork(binding.image, item.img?:"", ImageRequest.CacheChoice.DEFAULT)
            }
        }
    }

    private fun onBindImage(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ){
        if(imageList != null){
            holder?.let {
                val binding = ItemPlaceDetailBannerImageBinding.bind(holder.itemView)
                binding.banner.setAdapter(PlaceDetailActivity.ImageAdapter())
                binding.banner.setAutoPlay(false)
                binding.banner.setCanLoop(false)
                binding.banner.setOffScreenPageLimit(5)
                binding.banner.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL)
                binding.banner.setIndicatorVisibility(View.GONE)
                binding.banner.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                    override fun onPageSelected(position: Int) {
                        super.onPageSelected(position)
                        updateBannerIndex(position, binding)
                        if((imageList?.size?:0) > 1){
                            if(position == (imageList?.size?:0) - 1){
                                binding.banner.isCustomDisallowInterceptTouchEvent = true
                                binding.banner.customDisallowInterceptTouchEvent = true
                            } else {
                                binding.banner.isCustomDisallowInterceptTouchEvent = false
                            }
                        } else {
                            binding.banner.isCustomDisallowInterceptTouchEvent = false
                        }
                        onImageBannerPageSelected?.invoke(position, (imageList?.size?:0))
                    }
                })
                binding.banner.pageClick { clickedView, position ->
                    if(imageList?.isNotEmpty() == true){
                        val list = ArrayList<String>()
                        for (item in imageList!!){
                            list.add(item.url?:"")
                        }
                        PicturePreviewActivity.open(GlobalContextManager.appContext!!, list, position)
                    }
                }
                binding.banner.create(imageList)
                updateBannerIndex(0, binding)

                binding.bannerReactionView.updateBehavior(onSetting = {
                    it?.onNestedPreScroll = { coordinatorLayout: CoordinatorLayout,
                                              child: View,
                                              target: View,
                                              dx: Int,
                                              dy: Int,
                                              consumed: IntArray,
                                              type: Int ->
                        if(binding.banner.currentItem == (imageList?.size?:0) - 1){
                            onNestedPreScroll?.invoke(coordinatorLayout,
                                child,
                                target,
                                dx,
                                dy,
                                consumed,
                                type)
                        }
                    }
                    it?.onStopNestedScroll = onStopNestedScroll
                })
            }
        }
    }

    @SuppressLint("SetTextI18n")
    fun updateBannerIndex(position: Int, binding:ItemPlaceDetailBannerImageBinding){
        currentImagePosition = position
        if((imageList?.size?:0) <= 1 || vr != null){
            binding.bannerIndex.visibility = View.GONE
        } else {
            binding.bannerIndex.visibility = View.VISIBLE
            binding.bannerIndex.text = "${position + 1}/${imageList?.size}"
        }
        adapter.onImagePageChangeCallback?.invoke(position, imageList?.size?:0)
    }

}